package com.ruoyi.web.controller.system;

import com.ruoyi.bus.domain.BusEnterprise;
import com.ruoyi.bus.domain.BusMonitor;
import com.ruoyi.bus.service.IBusEnterpriseService;
import com.ruoyi.bus.service.IBusMonitorService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2021-09-06
 */
@Controller
@RequestMapping("/bus/enterprise")
public class BusEnterpriseController extends BaseController
{
    private String prefix = "bus/enterprise";

    @Autowired
    private IBusEnterpriseService busEnterpriseService;

    @Autowired
    private IBusMonitorService busMonitorService;

    @RequiresPermissions("bus:enterprise:view")
    @GetMapping()
    public String enterprise()
    {
        return prefix + "/enterprise";
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("bus:enterprise:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BusEnterprise busEnterprise)
    {
        startPage();
        List<BusEnterprise> list = busEnterpriseService.selectBusEnterpriseList(busEnterprise);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("bus:enterprise:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BusEnterprise busEnterprise)
    {
        List<BusEnterprise> list = busEnterpriseService.selectBusEnterpriseList(busEnterprise);
        ExcelUtil<BusEnterprise> util = new ExcelUtil<BusEnterprise>(BusEnterprise.class);
        return util.exportExcel(list, "【请填写功能名称】数据");
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    @RequiresPermissions("bus:enterprise:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BusEnterprise busEnterprise)
    {
        return toAjax(busEnterpriseService.insertBusEnterprise(busEnterprise));
    }

    /**
     * 修改【请填写功能名称】
     */
    @GetMapping("/edit/{entId}")
    public String edit(@PathVariable("entId") Long entId, ModelMap mmap)
    {
        BusEnterprise busEnterprise = busEnterpriseService.selectBusEnterpriseByEntId(entId);
        mmap.put("busEnterprise", busEnterprise);
        return prefix + "/edit";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @RequiresPermissions("bus:enterprise:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BusEnterprise busEnterprise)
    {
        return toAjax(busEnterpriseService.updateBusEnterprise(busEnterprise));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("bus:enterprise:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {

        if(null != ids){
            String[] idArray = ids.split(",");
            for (String endId: idArray
            ) {
               List<BusMonitor>  list =busMonitorService.findBusMonitorByEntId(endId);
               if(list.size()>0) {
                   return toAjax(-1);
               }
            }
        }


        return toAjax(busEnterpriseService.deleteBusEnterpriseByEntIds(ids));
    }
}
